package com.ylxl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylxl.common.constant.UserConstants;
import com.ylxl.common.domain.SysUser;
import com.ylxl.common.exception.BaseException;
import com.ylxl.common.utils.SecurityUtils;
import com.ylxl.common.utils.StringUtils;
import com.ylxl.entity.*;
import com.ylxl.entity.request.*;
import com.ylxl.mapper.MateriaProgramMapper;
import com.ylxl.mapper.ProgramMapper;
import com.ylxl.mapper.SysUserMapper;
import com.ylxl.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * 用户 业务层处理
 *
 */
@Service
public class ProgramServiceImpl extends ServiceImpl<ProgramMapper, Program>  implements IProgramService {
    private static final Logger log = LoggerFactory.getLogger(ProgramServiceImpl.class);

    @Autowired
    private ProgramMapper programMapper;
    @Autowired
    private MateriaProgramMapper materiaProgramMapper;


    @Override
    public List<Program> selectProgramList(QueryProgram queryProgram){
        LambdaQueryWrapper<Program> programLambdaQueryWrapper = new LambdaQueryWrapper<>();
        programLambdaQueryWrapper.like(StringUtils.isNotEmpty(queryProgram.getProgramName()),Program::getProgramName,queryProgram.getProgramName())
                .ge(queryProgram.getCreateTime() !=null,Program::getCreateTime,queryProgram.getCreateTime() )
                .le(queryProgram.getEndTime()!=null,Program::getCreateTime,queryProgram.getEndTime());
        return programMapper.selectList(programLambdaQueryWrapper);
    }

    @Override
    public int insertProgram(ProgramCmdOrMdy programCmdOrMdy) {
        Program program = new Program();
        BeanUtil.copyProperties(programCmdOrMdy, program);//属性拷贝
        programMapper.insert(program);
        deleteMP(program.getId());
        //插入中间表
        List<Integer> materialId = programCmdOrMdy.getMaterialId();
        materialId.forEach(f->{
            MateriaProgram materiaProgram = new MateriaProgram();
            materiaProgram.setMateriaId(f);
            materiaProgram.setProgramId(program.getId());
            materiaProgramMapper.insert(materiaProgram);
        });
        return programMapper.insert(program);
    }

    @Override
    public int updateProgram(ProgramCmdOrMdy programCmdOrMdy) {
        if(StringUtils.isEmpty(programCmdOrMdy.getId())){
            throw new BaseException("节目id不能为空");
        }
        deleteMP(programCmdOrMdy.getId());
        Program program = new Program();
        BeanUtil.copyProperties(programCmdOrMdy, program);//属性拷贝
        //插入中间表
        List<Integer> materialId = programCmdOrMdy.getMaterialId();
        materialId.forEach(f->{
            MateriaProgram materiaProgram = new MateriaProgram();
            materiaProgram.setMateriaId(f);
            materiaProgram.setProgramId(program.getId());
            materiaProgramMapper.insert(materiaProgram);
        });
        return programMapper.updateById(program);
    }

    @Override
    public Program selctById(Integer id) {
        if(StringUtils.isEmpty(id)){
            throw new BaseException("id不能为空");
        }
        return programMapper.selectById(id);
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        return programMapper.deleteBatchIds(ids);
    }
    public void deleteMP(Integer programId){
        LambdaUpdateWrapper<MateriaProgram> materiaProgramLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        materiaProgramLambdaUpdateWrapper
                .eq(MateriaProgram::getProgramId,programId);
        materiaProgramMapper.delete(materiaProgramLambdaUpdateWrapper);
    }
}
